package gr.aueb.mscis.chord;

import android.os.Message;
import android.util.Log;
import gr.aueb.mscis.Activities.ContentNodesActivity;

public class StabilizeThread extends Thread {

	Node node;

	public StabilizeThread(Node thisnode) {
		node = thisnode;
	}

	public void run() {
		while (!this.isInterrupted()) {
			node.stabilize();
			Message msg;
			msg = ContentNodesActivity.handlerGlobal.obtainMessage();
			msg.obj = "Trexw, dn psofisa";
			ContentNodesActivity.handlerGlobal.sendMessage(msg);
			try {
				msg = ContentNodesActivity.handlerGlobal.obtainMessage();
				msg.obj = "Stab My successor is  " + ":"
						+ node.getSuccessor().getAddr().toString()
						+ "My pred is  " + ":"
						+ node.getPredecessor().getAddr().toString();
				ContentNodesActivity.handlerGlobal.sendMessage(msg);
			} catch (NullPointerException npe) {
				if (node.getPredecessor() == null && node.getSuccessor() != null) {
					Log.v("stabilize", "I have no predecessor :(");
					msg = ContentNodesActivity.handlerGlobal.obtainMessage();
					msg.obj = "Stab My successor is " + ":"
							+ node.getSuccessor().getAddr().toString()
							+ " My pred is null";
					ContentNodesActivity.handlerGlobal.sendMessage(msg);
				} else {
					Log.v("stabilize", "I have neither a predecessor nor a successor :(((");
					msg = ContentNodesActivity.handlerGlobal.obtainMessage();
					msg.obj = "Stab My successor is null" + " My pred is null";
					ContentNodesActivity.handlerGlobal.sendMessage(msg);
				}
			}

			try {
				Thread.sleep(800);
			} catch (InterruptedException e) {

			}
		}
	}

}
